//
//  ViewController.m
//  autostainer
//
//  Created by lurenjia on 2017/5/13.
//  Copyright © 2017年 walker. All rights reserved.
//

#import "MainViewController.h"
#import <objc/runtime.h>
#import "Global.h"

@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource,sectionHeaderViewDelegate,PopOverViewDelegate>

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) SecionHeaderView *headerView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) PopOverView *popView;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.dataArray = @[@"DNA复染",@"HE染色",@"巴氏染色",@"Feulgen染色",@"Feulgen复染",@"用户自定义1",@"用户自定义2"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ModelFile" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *arr = dict[@"data"];
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSDictionary *dict in arr) {
        DataModel *model = [[DataModel alloc]initWithDict:dict];
        [arrM addObject:model];
    }
    self.dataArray = arrM.copy;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    NSMutableArray *hourArr = [NSMutableArray array];
    NSMutableArray *timeArr = [NSMutableArray array];
    NSMutableArray *temperatureArr = [NSMutableArray array];
    for (int i=0; i<25; i++) {
        NSString *str = [NSString stringWithFormat:@"%02d",i];
        [hourArr addObject:str];
    }
    [AppDelegate getInstance].hourArray = hourArr.copy;
    for (int i=0; i<60; i++) {
        NSString *str = [NSString stringWithFormat:@"%02d",i];
        [timeArr addObject:str];
    }
    [AppDelegate getInstance].timeArray = timeArr.copy;
    for (int i=-100; i<101; i++) {
        NSString *str = [NSString stringWithFormat:@"%d",i];
        [temperatureArr addObject:str];
    }
    [AppDelegate getInstance].temperatureArray = temperatureArr.copy;
}
- (IBAction)BLETestAction:(UIBarButtonItem *)sender {
    ViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ViewController"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    DataModel *dataModel = self.dataArray[section];
    NSInteger count = dataModel.isOpen ? dataModel.experimentDetails.count : 0;
    return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (indexPath.row == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell" forIndexPath:indexPath];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell" forIndexPath:indexPath];
        UILabel *nameLabel = [cell.contentView viewWithTag:1];
        UILabel *timeLabel = [cell.contentView viewWithTag:2];
        UILabel *noteLabel = [cell.contentView viewWithTag:3];
        
        DataModel *datamodel = self.dataArray[indexPath.section];
        ExperimentModel *model = datamodel.experimentDetails[indexPath.row];
        nameLabel.text = model.name;
        timeLabel.text = model.time;
        if (![model.note isEqualToString:@"0"]) {
            noteLabel.text = [NSString stringWithFormat:@"%@℃",model.note];
        }else {
            noteLabel.text = @"";
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    SecionHeaderView *headerView = [[NSBundle mainBundle]loadNibNamed:@"SecionHeaderView" owner:nil options:0].lastObject;
    headerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
    headerView.delegate = self;
    DataModel *model = self.dataArray[section];
    headerView.experimentNameLabel.text = model.experimentName;
    headerView.experimentTimeLabel.text = model.experimentTime;
    headerView.btn.tag = section + 5;
    if (model.isOpen) {
        CGAffineTransform currentTransform = headerView.iconView.transform;
        CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, M_PI/2);
        headerView.iconView.transform = newTransform;
    } else {
        CGAffineTransform currentTransform = headerView.iconView.transform;
        CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, -M_PI/2);
        headerView.iconView.transform = newTransform;
    }
    objc_setAssociatedObject(headerView.btn, @"btnKey", headerView.iconView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return headerView;
}

- (void)buttonPress:(UIButton *)sender {
    DataModel *model = _dataArray[sender.tag - 5];
    UIImageView *imageView = objc_getAssociatedObject(sender, @"btnKey");
    if (model.isOpen) {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
            CGAffineTransform currentTransform = imageView.transform;
            CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, -M_PI/2);
            imageView.transform = newTransform;
        } completion:nil];
    } else {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
            CGAffineTransform currentTransform = imageView.transform;
            CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, M_PI/2);
            imageView.transform = newTransform;
        } completion:nil];
    }
    
    model.isOpen = !model.isOpen;
    
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag - 5] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (IBAction)AddBtnClicked:(UIBarButtonItem *)sender {
    self.popView = [[PopOverView alloc]init];
    self.popView.backgroundViewDelegate = self;
    [self.popView show];
}

- (void)popOverView:(PopOverView *)popOverView onPopBtnClicked:(UIButton *)sender {
    [self.popView hide:true];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (sender.tag == 7) {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            CustomReactionViewController *vc = [sb instantiateViewControllerWithIdentifier:@"CustomReactionViewController"];
            vc.navTitleStr = @"用户自定义类型1";
            [self.navigationController pushViewController:vc animated:true];
        }
        else if (sender.tag == 8){
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            CustomReactionViewController *vc = [sb instantiateViewControllerWithIdentifier:@"CustomReactionViewController"];
            vc.navTitleStr = @"用户自定义类型2";
            [self.navigationController pushViewController:vc animated:true];
        }
        else {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        ReagentDetailViewController *VC = [sb instantiateViewControllerWithIdentifier:@"ReagentDetailViewController"];
            VC.experimentType = sender.tag - 1;
        [self.navigationController pushViewController:VC animated:true];
        }
    });
}
@end
